//package com.fy.config;
//
//import com.alibaba.druid.support.json.JSONUtils;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.fy.annotion.Log;
//import com.fy.common.JwtUtils;
//import com.fy.common.LogConstants;
//import com.fy.entity.BaseEntity;
//import com.fy.entity.SysLog;
//import com.fy.service.SysLogService;
//import com.fy.service.URoleOrganizationService;
//import com.fy.utils.IpUtils;
//import com.fy.utils.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.util.Date;
//
//
///**
// * @Description: 后台用户日志
// * @Author: www
// * @Date: 2021/06/22 16:09
// */
//@Aspect
//@Component
//@Order(3)
//@Slf4j
//public class UserLogAspect {
//
//    @Autowired
//    private URoleOrganizationService uRoleOrganizationService;
//
//    @Autowired
//    private SysLogService sysLogService;
//
//
//    @Pointcut("execution( * com.fy.controller..*.*(..))")
//    public void anyMethod() {
//    }
//
//
//
//    @Before("anyMethod()")
//    public Object before(JoinPoint joinPoint) {
//        Class clazz = joinPoint.getTarget().getClass();
//
//        //处理 del
//        Log annotation = (Log) clazz.getAnnotation(Log.class);
//        if (annotation != null) {
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            Method method = signature.getMethod();
//            if(!method.getName().contains("del")){
//               return joinPoint;
//            }
//            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            HttpServletRequest request = servletRequestAttributes.getRequest();
//            String action = request.getMethod();
//            String uri = request.getRequestURI();
//            log.info("action:[{}],uri:[{}]",action,uri);
//            String token = request.getHeader("token");
//            if(StringUtils.isBlank(token)){
//                try {
//                    return joinPoint;
//                } catch (Throwable throwable) {
//                    throwable.printStackTrace();
//                    log.error("执行方法出错:[{}],[{}]", joinPoint.getSignature().getName(), throwable.getMessage());
//                    throw new RuntimeException("方法执行失败");
//                }
//            }
//            //修改参数
//            Object[] args = joinPoint.getArgs();
////            BaseEntity baseEntity = null;
//            Integer id = 0;
//            for (int i = 0; i < args.length; i++) {
////                if(args[i] instanceof BaseEntity){
////                    baseEntity = (BaseEntity) args[i];
////                }
//                if(args[i] instanceof Integer){
//                    id = (Integer) args[i];
//                }
//            }
//
//
//            String userName = JwtUtils.getUserInfo(token).getUserName();
//            Integer userId = JwtUtils.getUserInfo(token).getId();
//            SysLog sysLog = new SysLog();
//            sysLog.setCreateTime(new Date());
//            sysLog.setUserName(userName);
//            sysLog.setUserId(userId);
//            sysLog.setLogType("删除");
//            sysLog.setLogContent("删除了"+annotation.value()+"（ID:"+id+"）");
//            sysLogService.save(sysLog);
//        }
//        return joinPoint;
//    }
//
//    @After("anyMethod()")
//    public Object after(JoinPoint joinPoint) {
//        Class clazz = joinPoint.getTarget().getClass();
//
//        //处理 del
//        Log annotation = (Log) clazz.getAnnotation(Log.class);
//        if (annotation != null) {
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            Method method = signature.getMethod();
//            if(!method.getName().contains("save")){
//                return joinPoint;
//            }
//            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            HttpServletRequest request = servletRequestAttributes.getRequest();
//            String action = request.getMethod();
//            String uri = request.getRequestURI();
//            log.info("action:[{}],uri:[{}]",action,uri);
//            String token = request.getHeader("token");
//            if(StringUtils.isBlank(token)){
//                try {
//                    return joinPoint;
//                } catch (Throwable throwable) {
//                    throwable.printStackTrace();
//                    log.error("执行方法出错:[{}],[{}]", joinPoint.getSignature().getName(), throwable.getMessage());
//                    throw new RuntimeException("方法执行失败");
//                }
//            }
//            //修改参数
//            Object[] args = joinPoint.getArgs();
////            BaseEntity baseEntity = null;
//            Integer id = 0;
//            for (int i = 0; i < args.length; i++) {
////                if(args[i] instanceof BaseEntity){
////                    baseEntity = (BaseEntity) args[i];
////                }
//                if(args[i] instanceof Integer){
//                    id = (Integer) args[i];
//                }
//            }
//
//
//            String userName = JwtUtils.getUserInfo(token).getUserName();
//            Integer userId = JwtUtils.getUserInfo(token).getId();
//            SysLog sysLog = new SysLog();
//            sysLog.setCreateTime(new Date());
//            sysLog.setUserName(userName);
//            sysLog.setUserId(userId);
//            sysLog.setLogType("删除");
//            sysLog.setLogContent("删除了"+annotation.value()+"（ID:"+id+"）");
//            sysLogService.save(sysLog);
//        }
//        return joinPoint;
//    }
//
//}